In your final repo, there should be an R markdown file that organizes all computational steps for evaluating your proposed Facial Expression Recognition framework.
This file is currently a template for running evaluation experiments. You should update it according to your codes but following precisely the same structure.
set.seed(2020)
Provide directories for training images. Training images and Training fiducial points will be in different subfolders.
train_dir <- "../data/train_set/" # This will be modified for different data sets.
train_image_dir <- paste(train_dir, "images/", sep= "")
train_pt_dir <- paste(train_dir, "points/", sep= "")
train_label_path <- paste(train_dir, "label.csv", sep= "")
In this chunk, we have a set of controls for the evaluation experiments.
run.cv <- TRUE # run cross-validation on the training set
sample.reweight <- TRUE # run sample reweighting in model training
K <- 5 # number of CV folds
run.feature.train <- TRUE # process features for training set
run.test <- TRUE # run evaluation on an independent test set
run.feature.test <- TRUE # process features for test set
By Using cross-validation, we want to use different specifications to compare the performance of models. We setup t as tune parameter for GBM.
k = c(50,100,150,200,250,300)
model_labels = paste("GBM with k =", k)
#train-test split
info <- read.csv(train_label_path)
n <- nrow(info)
n_train <- round(n*(4/5), 0)
train_idx <- sample(info$Index, n_train, replace = F)
test_idx <- setdiff(info$Index, train_idx)
If you choose to extract features from images, such as using Gabor filter, R memory will exhaust all images are read together. The solution is to repeat reading a smaller batch(e.g 100) and process them.
n_files <- length(list.files(train_image_dir))
image_list <- list()
for(i in 1:100){
image_list[[i]] <- readImage(paste0(train_image_dir, sprintf("%04d", i), ".jpg"))
}
Fiducial points are stored in matlab format. In this step, we read them and store them in a list.
# #function to read fiducial points
# #input: index
# #output: matrix of fiducial points corresponding to the index
# readMat.matrix <- function(index){
# return(round(readMat(paste0(train_pt_dir, sprintf("%04d", index), ".mat"))[[1]],0))
# }
# #load fiducial points
# fiducial_pt_list <- lapply(1:n_files, readMat.matrix)
# save(fiducial_pt_list, file="../output/fiducial_pt_list.RData")
load("../output/fiducial_pt_list.RData")
The follow plots show how pairwise distance between fiducial points can work as feature for facial emotion recognition.
Figure1
feature.R should be the wrapper for all your feature engineering functions and options. The function feature( ) should have options that correspond to different scenarios for your project and produces an R object that contains features and responses that are required by all the models you are going to evaluate later.
feature.Rsource("../lib/feature.R")
tm_feature_train <- NA
if(run.feature.train){
tm_feature_train <- system.time(dat_train <- feature(fiducial_pt_list, train_idx))
save(dat_train, file="../output/feature_train.RData")
}else{
load(file="../output/feature_train.RData")
}
tm_feature_test <- NA
if(run.feature.test){
tm_feature_test <- system.time(dat_test <- feature(fiducial_pt_list, test_idx))
save(dat_test, file="../output/feature_test.RData")
}else{
load(file="../output/feature_test.RData")
}
load(file="../output/feature_test.RData")
load(file="../output/feature_train.RData")
Same as Baseline model
Using cross-validation, we compare the performance of models with different specifications. In the following chunk of code, we tune parameter cost_svm (number of shrinkage) for the Support vector machine (SVM)
cost_svm = c(0.00001, 0.0001, 0.001, 0.01, 0.1)
model_labels_svm = paste("SVM with cost =", cost_svm)
model_labels_svm
## [1] "SVM with cost = 1e-05" "SVM with cost = 1e-04" "SVM with cost = 0.001"
## [4] "SVM with cost = 0.01" "SVM with cost = 0.1"
source("../lib/SVM_model.R")
# SVM Cross-validation
cost = c(0.00001, 0.0001, 0.001, 0.01, 0.1)
model_labels_svm = paste("SVM with cost =", cost)
model_labels_svm
err_svm <- matrix(0, nrow = length(cost), ncol = 2)
for(i in 1:length(cost)){
print(paste("cost=", cost[i]))
err_svm[i,] <- CV_SVM(dat_train, K=5, cost[i])
save(err_svm, file="../output/err_svm.RData")
}
#Load visualization of cross validation results of svm
load("../output/err_svm.RData")
err_svm <- as.data.frame(err_svm)
colnames(err_svm) <- c("mean_error", "sd_error")
cost = c(0.00001, 0.0001, 0.001, 0.01, 0.1)
err_svm$cost = as.factor(cost)
err_svm %>% ggplot(aes(x = cost, y = mean_error, ymin = mean_error - sd_error, ymax = mean_error + sd_error)) +
geom_crossbar() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
Find the best cost for SVM model
cost_best_svm <- cost[which.min(err_svm[,1])]
#par_best_svm <- list(cost = cost_best_svm)
# Training
tm_train_svm = NA
tm_train_svm <- system.time(fit_train_svm <- svm(label ~., data = dat_train, kernel = "linear", cost = cost_best_svm) )
#Save and load model
saveRDS(fit_train_svm, "../output/fit_train_svm.RDS")
fit_train_svm <- readRDS("../output/fit_train_svm.RDS")
# Testing
tm_test_svm=NA
tm_test_svm <- system.time(pred_svm <- predict(fit_train_svm, dat_test))
# Evaluation
accu_svm <- mean(dat_test$label == pred_svm)
real_label = dat_test$label %>% as.character() %>% as.numeric()
pred_value_svm = pred_svm %>% as.character() %>% as.numeric()
confusionMatrix(pred_svm,dat_test$label)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 467 98
## 1 6 29
##
## Accuracy : 0.8267
## 95% CI : (0.794, 0.8561)
## No Information Rate : 0.7883
## P-Value [Acc > NIR] : 0.01096
##
## Kappa : 0.2934
##
## Mcnemar's Test P-Value : < 2e-16
##
## Sensitivity : 0.9873
## Specificity : 0.2283
## Pos Pred Value : 0.8265
## Neg Pred Value : 0.8286
## Prevalence : 0.7883
## Detection Rate : 0.7783
## Detection Prevalence : 0.9417
## Balanced Accuracy : 0.6078
##
## 'Positive' Class : 0
##
cost_choose <- cost[which.min(err_svm[,1])]
cat("The accuracy of model: cost =", cost_choose, "is", accu_svm*100, "%.\n")
## The accuracy of model: cost = 0.001 is 82.66667 %.
# The AUC for SVM model
AUC_SVM = auc_roc(real_label, pred_value_svm)
AUC_SVM
## [1] 0.8275601
Prediction performance matters, so does the running times for constructing features and for training the model, especially when the computation resource is limited.
Model_performace <- function(time_feature_train, time_feature_test, time_train, time_test){
cat("Time for constructing training features=", time_feature_train[1], "s \n")
cat("Time for constructing testing features=", time_feature_test[1], "s \n")
cat("Time for training model=", time_train[1], "s \n")
cat("Time for testing model=", time_test[1], "s \n")
}
cat("The accuracy of the SVM model: cost =", cost[which.min(err_svm[,1])], "is", accu_svm*100, "%.\n")
## The accuracy of the SVM model: cost = 0.001 is 82.66667 %.
cat("The auc value for the SVM model is",AUC_SVM * 100, "%.\n")
## The auc value for the SVM model is 82.75601 %.
Model_performace(tm_feature_train, tm_feature_test, tm_train_svm, tm_test_svm)
## Time for constructing training features= 0.994 s
## Time for constructing testing features= 0.192 s
## Time for training model= 94.734 s
## Time for testing model= 8.385 s
Feature extraction is the same as Baseline model, and we improved the feature by using Principal Components Analysis for feature selection.
feature_improved.R should be the wrapper for all your feature engineering functions and options. The function feature_improved() should have options that correspond to different scenarios for your project and produces an R object that contains features and responses that are required by all the models you are going to evaluate later.
+ feature_improved.R
+ Input: train/test data
+ Output: an RData file that contains extracted features and corresponding responses
source("../lib/feature_improved.R") # change file name
data_test_imp <- dat_test[ ,-ncol(dat_test)]
data_train_imp <- dat_train[ ,-ncol(dat_train)]
tm_feature_train_imp <- NA
if(run.feature.train){
tm_feature_train_imp <- system.time(data_train_imp <- feature_improved(data_train_imp, index = NULL))
}
index_train_pca <- ncol(data_train_imp)
tm_feature_test_improved <- NA
if(run.feature.test){
tm_feature_test_imp <- system.time(data_test_imp <- feature_improved(data_test_imp, index = index_train_pca))
}
save(data_train_imp, file="../output/feature_train_imp.RData")
save(data_test_imp, file="../output/feature_test_imp.RData")
data_train_imp <- as.data.frame(data_train_imp)
data_train_imp$label <- dat_train$label
colnames(data_train_imp)
## [1] "PC1" "PC2" "PC3" "PC4" "PC5" "PC6" "PC7" "PC8" "PC9"
## [10] "PC10" "PC11" "PC12" "PC13" "PC14" "PC15" "PC16" "PC17" "PC18"
## [19] "PC19" "PC20" "PC21" "PC22" "PC23" "PC24" "PC25" "PC26" "PC27"
## [28] "PC28" "PC29" "PC30" "PC31" "label"
data_test_imp <- as.data.frame(data_test_imp)
data_test_imp$label <- dat_test$label
colnames(data_test_imp)
## [1] "PC1" "PC2" "PC3" "PC4" "PC5" "PC6" "PC7" "PC8" "PC9"
## [10] "PC10" "PC11" "PC12" "PC13" "PC14" "PC15" "PC16" "PC17" "PC18"
## [19] "PC19" "PC20" "PC21" "PC22" "PC23" "PC24" "PC25" "PC26" "PC27"
## [28] "PC28" "PC29" "PC30" "PC31" "label"
source("../lib/SVM_model_weighted.R")
#SVM Cross-validation
cost = c(0.00001,0.0001,0.001,0.01,0.1,1)
err_svm_imp <- matrix(0, nrow = length(cost), ncol = 2)
for(i in 1:length(cost)){
print(paste("cost:", cost[i]))
err_svm_imp[i,] <- CV_SVM_weight(data_train_imp, K = 5, cost[i])
saveRDS(err_svm_imp, file="../output/err_svm_imp.RDS")
}
err_svm_imp
cost_best_svm_imp <- cost[which.min(err_svm_imp[,1])]
cost_best_svm_imp
From err_svm_imp, we obtained that the cost didn’t influence the error mean and the error’s standard deviation
cost_best_svm_imp = 0.01
# Training
tm_train_svm_imp = NA
temp <- ovun.sample(label ~ ., data = data_train_imp, method = "over", p = 0.3)$data
tm_train_svm_imp <- system.time(fit_train_svm_imp <- svm(label ~., data = temp, kernel = "linear", cost = cost_best_svm_imp) )
#Save and load model
saveRDS(fit_train_svm_imp, "../output/fit_train_svm_imp.RDS")
fit_train_svm_imp <- readRDS("../output/fit_train_svm_imp.RDS")
# Testing
tm_test_svm_imp = NA
tm_test_svm_imp <- system.time(pred_svm_imp <- predict(fit_train_svm_imp, data_test_imp))
# Evaluation
accu_svm_imp <- mean(dat_test$label == pred_svm_imp)
accu_svm_imp
## [1] 0.7616667
pred_value_svm_imp = pred_svm_imp %>% as.character() %>% as.numeric()
real_label = dat_test$label %>% as.character() %>% as.numeric()
confusionMatrix(pred_svm_imp, dat_test$label)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 446 116
## 1 27 11
##
## Accuracy : 0.7617
## 95% CI : (0.7255, 0.7952)
## No Information Rate : 0.7883
## P-Value [Acc > NIR] : 0.9488
##
## Kappa : 0.0397
##
## Mcnemar's Test P-Value : 1.854e-13
##
## Sensitivity : 0.94292
## Specificity : 0.08661
## Pos Pred Value : 0.79359
## Neg Pred Value : 0.28947
## Prevalence : 0.78833
## Detection Rate : 0.74333
## Detection Prevalence : 0.93667
## Balanced Accuracy : 0.51477
##
## 'Positive' Class : 0
##
print(paste("The accuracy of model: cost =", cost_best_svm_imp, "is", accu_svm_imp * 100, "%"))
## [1] "The accuracy of model: cost = 0.01 is 76.1666666666667 %"
# The AUC for SVM model
AUC_SVM_imp = auc_roc(real_label, pred_value_svm_imp)
AUC_SVM_imp
## [1] 0.541534
Prediction performance matters, so does the running times for constructing features and for training the model, especially when the computation resource is limited.
print(paste("The accuracy of the SVM improved model: cost =", cost_best_svm_imp , "is", accu_svm_imp * 100, "%"))
## [1] "The accuracy of the SVM improved model: cost = 0.01 is 76.1666666666667 %"
print(paste("The auc value for tje SVM improved model is", AUC_SVM_imp * 100, "%"))
## [1] "The auc value for tje SVM improved model is 54.1533995130174 %"
Model_performace(tm_feature_train_imp, tm_feature_test_imp, tm_train_svm_imp, tm_test_svm_imp)
## Time for constructing training features= 170.779 s
## Time for constructing testing features= 9.538 s
## Time for training model= 0.719 s
## Time for testing model= 0.054 s
###Reference - Du, S., Tao, Y., & Martinez, A. M. (2014). Compound facial expressions of emotion. Proceedings of the National Academy of Sciences, 111(15), E1454-E1462.